import {
  employeeManagement,
  staff_btn
} from '../../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lis: [],
    clientX:'',
    clientY:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    employeeManagement({
      token: app.globalData.token
    }).then(res => {
      if (res.data.code == 666) {
        that.setData({
          lis: res.data.data
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        return false;
      }
      for (var i = 0; i < that.data.lis.length ; i++){
        that.data.lis[i].isTouchMove =false
      }
      console.log(that.data.lis)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '赋能奢商，只为业绩增长',
      imageUrl: "https://document.growthupup.com/growthShare.jpg?t=" + new Date().getTime(),
      path: '/pages/user/login/login',
    }
  },
  lis_btn: function(e) {
    var ids = e.target.dataset.id;
    var lis = this.data.lis;
    var state = e.target.dataset.state;
    var that = this
    staff_btn({
      token: app.globalData.token,
      id: ids,
      state: state
    }).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: res.data.msg,
        })
        for (var i = 0; i < lis.length; i++) {
          if (lis[i].id == ids) {
            var state = lis[i].state
            if (state == 1) {
              lis[i].state = 0,
              lis[i].isTouchMove = false
            } else {
              lis[i].state = 1
              lis[i].isTouchMove = false
            }
            that.setData({
              lis: lis
            })
          }
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  touchS:function(e){
    this.setData({
      clientX: e.changedTouches[0].clientX,
      clientY: e.changedTouches[0].clientY,
    })
  },
  touchM:function(e){
    var clX = this.data.clientX;
    var clY = this.data.clientY;
    var ind = e.currentTarget.dataset.index
    var that = this
    var touchMoveX = e.changedTouches[0].clientX;
    var touchMoveY = e.changedTouches[0].clientY
    var angle = that.angle({ X: clX, Y: clY }, { X: touchMoveX, Y: touchMoveY });
    if (Math.abs(angle) > 5){
      return;
    }
    if (clX - e.touches[0].clientX > 0){
        that.data.lis[ind].isTouchMove = true;
        that.setData({
          lis:that.data.lis
        })
    } else if (e.touches[0].clientX - clX > 0){
      that.data.lis[ind].isTouchMove = false;
      that.setData({
        lis: that.data.lis
      })
    }
  },
  angle: function (start, end) {
    var _X = end.X - start.X,
        _Y = end.Y - start.Y
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
})